package com.winway.sixbook.custom;

import java.text.DecimalFormat;

import com.winway.sixbook.R;

import android.R.integer;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * 自定义SimpleCursorAdapter
 * @author Administrator
 *
 */
public class CustomSca extends SimpleCursorAdapter{
	 //声明LayoutInflater用于查找XML布局
	 private LayoutInflater mInflater; 
	 public CustomSca(Context context, int layout, Cursor c, String[] from, int[] to) {
		 super(context, layout, c, from, to);
	 }
	 /*
	  * 重写此方法
	  */
	 @Override
	 public void bindView(View view, Context context, Cursor cursor) {
		  RelativeLayout layout = null;
		  if(view==null)
		  {
			  layout = (RelativeLayout) mInflater.inflate(R.layout.counterlist_item,null);
		  }
		  else
		  {
			  layout = (RelativeLayout)view;
		  }
		  
		  TextView counterlist_item_digest = (TextView)layout.findViewById(R.id.counterlist_item_digest);
		  TextView counterlist_item_typename = (TextView)layout.findViewById(R.id.counterlist_item_typename);
		  TextView todaycountdata = (TextView)layout.findViewById(R.id.todaycountdata);
		  TextView hidenid = (TextView)layout.findViewById(R.id.hidenid);
		  TextView tv_begintime = (TextView)layout.findViewById(R.id.tv_kaishitime);
		  TextView tv_endtime = (TextView)layout.findViewById(R.id.tv_endtime);
		  TextView todaywc = (TextView)layout.findViewById(R.id.todaywc);
		  
		  String countname = cursor.getString(cursor.getColumnIndex("countname"));
		  String countnum = cursor.getString(cursor.getColumnIndex("countnum"));
		  String counttoday = cursor.getString(cursor.getColumnIndex("counttoday"));
		  String id = cursor.getString(cursor.getColumnIndex("_id"));
		  String begindate = cursor.getString(cursor.getColumnIndex("begindate"));
		  String enddate = cursor.getString(cursor.getColumnIndex("enddate"));
		  String targetnum = cursor.getString(cursor.getColumnIndex("targetnum"));
		  
		  int num=0,num1=0;
		  double ywc=0;
		  if(null != countnum && !"".equals(countnum)){
			  num = Integer.parseInt(countnum);
		  }
		  if(null != targetnum && !"".equals(targetnum)){
			  num1 = Integer.parseInt(targetnum);
		  }
		  if(num1==0){
			  ywc = 100;
		  }else{
			  ywc = (num*100 )/num1;
			  if(ywc>100){
				  ywc = 100;
			  }
		  }
		  
		  DecimalFormat df = new DecimalFormat("#.00");
		  counterlist_item_digest.setText(countname);
		  counterlist_item_typename.setText(countnum);
		  todaycountdata.setText(counttoday);
		  hidenid.setText(id);
		  tv_begintime.setText(begindate);
		  tv_endtime.setText(enddate);
		  todaywc.setText(String.format("%.1f", ywc)+"%");
	 } 
}